import { lazy, Suspense } from 'react';
import { createBrowserRouter, Navigate } from 'react-router-dom';
import AuthRoute from '../components/common/AuthRoute';

// 布局组件
const AdminLayout = lazy(() => import('../components/common/Layout/AdminLayout'));
const EmployeeLayout = lazy(() => import('../components/common/Layout/EmployeeLayout'));
const CandidateLayout = lazy(() => import('../components/common/Layout/CandidateLayout'));

// 公共页面
const Login = lazy(() => import('../pages/public/Login'));
const Register = lazy(() => import('../pages/public/Register'));
const PublicIndex = lazy(() => import('../pages/public/Index'));

// 管理员页面
const AdminDashboard = lazy(() => import('../pages/admin/Dashboard'));
const AdminEmployee = lazy(() => import('../pages/private/Employee'));
const AdminDepartment = lazy(() => import('../pages/private/Department'));
const AdminRecruitment = lazy(() => import('../pages/private/Recruitment'));
const AdminSalary = lazy(() => import('../pages/private/Salary'));
const AdminTraining = lazy(() => import('../pages/private/Training'));
const AdminAttendance = lazy(() => import('../pages/admin/Attendance'));
const AdminNotice = lazy(() => import('../pages/private/Notice/NoticeManagement'));

// 员工页面
const EmployeeDashboard = lazy(() => import('../pages/employee/Dashboard'));
const EmployeeProfile = lazy(() => import('../pages/private/Profile'));
const EmployeeAttendance = lazy(() => import('../pages/employee/Attendance'));
const EmployeeSalary = lazy(() => import('../pages/private/Salary/MySalaryHistory'));
const EmployeeTrainingList = lazy(() => import('../pages/private/Training/TrainingList'));
const EmployeeTrainingEnroll = lazy(() => import('../pages/private/Training/TrainingEnroll'));
const EmployeeMyTraining = lazy(() => import('../pages/private/Training/MyTraining'));
const EmployeeNotice = lazy(() => import('../pages/private/Notice'));

// 应聘者页面
const CandidateDashboard = lazy(() => import('../pages/candidate/Dashboard'));
const CandidateProfile = lazy(() => import('../pages/private/Profile'));
const CandidateResume = lazy(() => import('../pages/private/Resumes'));
const CandidateApplication = lazy(() => import('../pages/private/Application'));
const CandidateInterview = lazy(() => import('../pages/private/Interview'));
const CandidateRecruitment = lazy(() => import('../pages/private/Recruitment'));
const CandidateOffers = lazy(() => import('../pages/private/Offers'));

// 加载组件
const LoadingComponent = () => <div>加载中...</div>;

// 路由配置
const routes = [
  // 公共路由
  {
    path: '/login',
    element: (
      <Suspense fallback={<LoadingComponent />}>
        <Login />
      </Suspense>
    ),
  },
  {
    path: '/register',
    element: (
      <Suspense fallback={<LoadingComponent />}>
        <Register />
      </Suspense>
    ),
  },
  {
    path: '/public',
    element: (
      <Suspense fallback={<LoadingComponent />}>
        <PublicIndex />
      </Suspense>
    ),
  },
  
  // 管理员路由
  {
    path: '/admin',
    element: (
      <Suspense fallback={<LoadingComponent />}>
        <AuthRoute allowedRoles={['admin']}>
          <AdminLayout />
        </AuthRoute>
      </Suspense>
    ),
    children: [
      {
        path: '',
        element: <Navigate to="/admin/dashboard" />,
      },
      {
        path: 'dashboard',
        element: (
          <Suspense fallback={<LoadingComponent />}>
            <AdminDashboard />
          </Suspense>
        ),
      },
      {
        path: 'employee',
        element: (
          <Suspense fallback={<LoadingComponent />}>
            <AdminEmployee />
          </Suspense>
        ),
      },
      {
        path: 'department',
        element: (
          <Suspense fallback={<LoadingComponent />}>
            <AdminDepartment />
          </Suspense>
        ),
      },
      {
        path: 'recruitment',
        element: (
          <Suspense fallback={<LoadingComponent />}>
            <AdminRecruitment />
          </Suspense>
        ),
      },
      {
        path: 'salary',
        element: (
          <Suspense fallback={<LoadingComponent />}>
            <AdminSalary />
          </Suspense>
        ),
      },
      {
        path: 'training',
        element: (
          <Suspense fallback={<LoadingComponent />}>
            <AdminTraining />
          </Suspense>
        ),
      },
      {
        path: 'attendance',
        element: (
          <Suspense fallback={<LoadingComponent />}>
            <AdminAttendance />
          </Suspense>
        ),
      },
      {
        path: 'notice',
        element: (
          <Suspense fallback={<LoadingComponent />}>
            <AdminNotice />
          </Suspense>
        ),
      },
    ],
  },
  
  // 员工路由
  {
    path: '/employee',
    element: (
      <Suspense fallback={<LoadingComponent />}>
        <AuthRoute allowedRoles={['employee']}>
          <EmployeeLayout />
        </AuthRoute>
      </Suspense>
    ),
    children: [
      {
        path: '',
        element: <Navigate to="/employee/dashboard" />,
      },
      {
        path: 'dashboard',
        element: (
          <Suspense fallback={<LoadingComponent />}>
            <EmployeeDashboard />
          </Suspense>
        ),
      },
      {
        path: 'profile',
        element: (
          <Suspense fallback={<LoadingComponent />}>
            <EmployeeProfile />
          </Suspense>
        ),
      },
      {
        path: 'attendance',
        element: (
          <Suspense fallback={<LoadingComponent />}>
            <EmployeeAttendance />
          </Suspense>
        ),
      },
      {
        path: 'salary',
        element: (
          <Suspense fallback={<LoadingComponent />}>
            <EmployeeSalary />
          </Suspense>
        ),
      },
      {
        path: 'training',
        children: [
          {
            path: '',
            element: <Navigate to="/employee/training/list" />,
          },
          {
            path: 'list',
            element: (
              <Suspense fallback={<LoadingComponent />}>
                <EmployeeTrainingList />
              </Suspense>
            ),
          },
          {
            path: 'enroll',
            element: (
              <Suspense fallback={<LoadingComponent />}>
                <EmployeeTrainingEnroll />
              </Suspense>
            ),
          },
          {
            path: 'my',
            element: (
              <Suspense fallback={<LoadingComponent />}>
                <EmployeeMyTraining />
              </Suspense>
            ),
          },
        ],
      },
      {
        path: 'notice',
        element: (
          <Suspense fallback={<LoadingComponent />}>
            <EmployeeNotice />
          </Suspense>
        ),
      },
    ],
  },
  
  // 应聘者路由
  {
    path: '/candidate',
    element: (
      <Suspense fallback={<LoadingComponent />}>
        <AuthRoute allowedRoles={['candidate']}>
          <CandidateLayout />
        </AuthRoute>
      </Suspense>
    ),
    children: [
      {
        path: '',
        element: <Navigate to="/candidate/dashboard" />,
      },
      {
        path: 'dashboard',
        element: (
          <Suspense fallback={<LoadingComponent />}>
            <CandidateDashboard />
          </Suspense>
        ),
      },
      {
        path: 'profile',
        element: (
          <Suspense fallback={<LoadingComponent />}>
            <CandidateProfile />
          </Suspense>
        ),
      },
      {
        path: 'resume',
        element: (
          <Suspense fallback={<LoadingComponent />}>
            <CandidateResume />
          </Suspense>
        ),
      },
      {
        path: 'application',
        element: (
          <Suspense fallback={<LoadingComponent />}>
            <CandidateApplication />
          </Suspense>
        ),
      },
      {
        path: 'interview',
        element: (
          <Suspense fallback={<LoadingComponent />}>
            <CandidateInterview />
          </Suspense>
        ),
      },
      {
        path: 'recruitment',
        element: (
          <Suspense fallback={<LoadingComponent />}>
            <CandidateRecruitment />
          </Suspense>
        ),
      },
      {
        path: 'offers',
        element: (
          <Suspense fallback={<LoadingComponent />}>
            <CandidateOffers />
          </Suspense>
        ),
      },
    ],
  },
  
  // 默认路由
  {
    path: '/',
    element: <Navigate to="/login" />,
  },
  {
    path: '*',
    element: <Navigate to="/login" />,
  },
];

export default createBrowserRouter(routes);